package net.schmurgon.gwt.scaffold;

import com.google.gwt.app.place.Activity;
import com.google.gwt.app.place.ActivityMapper;
import com.google.gwt.app.place.Place;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.ProxyPlace;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.request.ApplicationEntityTypesProcessor;
import net.schmurgon.gwt.request.SeriesRecord;
import net.schmurgon.gwt.ui.SeriesActivitiesMapper;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.ui.RunnerActivitiesMapper;
import net.schmurgon.gwt.request.RunRecord;
import net.schmurgon.gwt.ui.RunActivitiesMapper;
import net.schmurgon.gwt.request.CourseRecord;
import net.schmurgon.gwt.ui.CourseActivitiesMapper;

/**
 * Instantiates detail activities.
 */
public class ApplicationDetailsActivities implements ActivityMapper {
	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;

	public ApplicationDetailsActivities(ApplicationRequestFactory requestFactory, PlaceController placeController) {
		this.requests = requestFactory;
		this.placeController = placeController;
	}

	public Activity getActivity(Place place) {
		if (!(place instanceof ProxyPlace)) {
			return null;
		}

		final ProxyPlace proxyPlace = (ProxyPlace) place;

		return new ApplicationEntityTypesProcessor<Activity>() {
			@Override
			public void handleSeries(SeriesRecord proxy) {
				setResult(new SeriesActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
			@Override
			public void handleRunner(RunnerRecord proxy) {
				setResult(new RunnerActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
			@Override
			public void handleRun(RunRecord proxy) {
				setResult(new RunActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
			@Override
			public void handleCourse(CourseRecord proxy) {
				setResult(new CourseActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
		}.process(proxyPlace.getProxy());
	}
}
